/*******************************************************************************
 * Copyright (c) 2009 Dop Sun.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dop Sun - initial API and implementation
 ******************************************************************************/
package com.softao.ming.gde.internal.core.launcher;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper;

/**
 * Generates the bootstrap application.
 * <p>
 * It creates the following details:
 * <ul>
 * <li>Source Folder Structure
 * <li>Module File
 * <li>Public HTML File
 * <li>Entry Point Class: including the registering the plugin.
 * </ul>
 */
@SuppressWarnings("restriction")
public class LaunchUtil {
	/**
	 * @param buildEntryStream
	 * @return source
	 */
	public static String[] getSourceFolders(InputStream buildEntryStream) {
		Properties prop = new Properties();
		try {
			prop.load(buildEntryStream);
		} catch (IOException e) {
			e.printStackTrace();
			return new String[0];
		}
		String source = prop.getProperty("source..");
		return source == null ? new String[0] : source.split(",");
	}
	
	/**
	 * @param configuration
	 * @return the workspace data location
	 */
	public static String getWorkspaceDataLocation(ILaunchConfiguration configuration) {
		try {
			return LaunchArgumentsHelper.getWorkingDirectory(configuration).getPath();
		} catch (CoreException e) {
			e.printStackTrace();
			throw new RuntimeException("Can not resolve the workspace data location.", e);
		}
	}
	
	/**
	 * @param configuration
	 * @return configuration location
	 */
	public static String getConfigurationLocation(ILaunchConfiguration configuration) {
		try {
			return configuration.getAttribute("configurationLocation", "");
		} catch (CoreException e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * @param path
	 * @return the container.
	 */
	public static IContainer getContainer(String path) {
		if (path.length() > 0) {
			IResource res = null;
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			if (path.startsWith("${workspace_loc:")) { //$NON-NLS-1$
				IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
				try {
					path = manager.performStringSubstitution(path, false);
					IContainer[] containers = root.findContainersForLocation(new Path(path));
					if (containers.length > 0)
						res = containers[0];
				} catch (CoreException e) {
				}
			} else {
				res = root.findMember(path);
			}
			if (res instanceof IContainer) {
				return (IContainer) res;
			}
		}
		return null;
	}

}
